package com.neoenox.neotwix;

import java.util.ArrayList;
import java.util.Locale;

import task.GetHomeTimeLine;
import twitter4j.Paging;
import util.TwitterUtil;
import adapter.TwitterTimelineAdapter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.neoenox.twitterapp.R;

import data.TwitterAcountDataList;
import data.TwitterStatusData;

public class TimeLineActivity extends FragmentActivity {

	public static final String ITEMS = "items";

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter = null;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	private TwitterAcountDataList twitterDataList = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timeline_main);
		// Twitterインスタンス読み込み
		Object extra = getIntent().getExtras().get("Twitter");
		if (extra instanceof TwitterAcountDataList) {
			Log.v("TimeLine", "TwitterAcountDataList Instance");
			twitterDataList = (TwitterAcountDataList) extra;
		}

		// Create the adapter that will return a fragment for each of the three
		// primary sections of the app.
		if (mSectionsPagerAdapter == null) {
			mSectionsPagerAdapter = new SectionsPagerAdapter(
					getSupportFragmentManager());
		}
		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.time_line, menu);
		return true;
	}

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			// getItem is called to instantiate the fragment for the given page.
			// Return a DummySectionFragment (defined as a static inner class
			// below) with the page number as its lone argument.
			switch (position) {
			case 0:
				return new DummySectionFragment();

			default:
				break;
			}
			return null;
		}

		@Override
		public int getCount() {
			return 1;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			switch (position) {
			case 0:
				return getString(R.string.title_section1).toUpperCase(l);
			case 1:
				return getString(R.string.title_section2).toUpperCase(l);
			case 2:
				return getString(R.string.title_section3).toUpperCase(l);
			}
			return null;
		}
	}

	/**
	 * A dummy fragment representing a section of the app, but that simply
	 * displays dummy text.
	 */
	public class DummySectionFragment extends Fragment {
		/**
		 * The fragment argument representing the section number for this
		 * fragment.
		 */
		public static final String ARG_SECTION_NUMBER = "section_number";
		private ArrayList<TwitterStatusData> items = new ArrayList<TwitterStatusData>();
		private ListView listView;
		private TwitterTimelineAdapter adapter;
		private GetHomeTimeLine getHomeTimeLine;
		private SwipeRefreshLayout mSwipeRefreshLayout;

		public DummySectionFragment() {
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

			setRetainInstance(true);
			adapter = new TwitterTimelineAdapter(getActivity(),
					R.layout.timeline_inline, items);
			Paging paging = new Paging(1, 100);
			getHomeTimeLine = new GetHomeTimeLine(items, adapter, paging);
			getHomeTimeLine.execute(twitterDataList.getTwitterAcountDataList()
					.get(TwitterUtil.loadAccountCnt(getActivity()))
					.getTwitter());
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {

			return inflater.inflate(R.layout.fragment_time_line_dummy,
					container, false);

		}

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);

			listView = (ListView) getView().findViewById(R.id.listView1);
			listView.setAdapter(adapter);

			mSwipeRefreshLayout = (SwipeRefreshLayout)getView().findViewById(R.id.refresh);
			mSwipeRefreshLayout.setColorSchemeResources(R.color.red, R.color.green, R.color.blue, R.color.yellow);
			mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

				@Override
				public void onRefresh() {
					// 3秒待機
					new Handler().postDelayed(new Runnable() {
						@Override
						public void run() {
							mSwipeRefreshLayout.setRefreshing(false);
						}
					}, 3000);
				}
			});

		}
	}
}
